package com.jhhc;

import com.jhhc.BDMS.System.Serivce.BDMS_POWER_BService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.TimeUtil;
import com.jhhc.Weathers.datasupplement.entity.BdmsWfProductF;
import com.jhhc.Weathers.datasupplement.entity.Params;
import com.jhhc.Weathers.datasupplement.entity.jcdzBO.ProductF;
import com.jhhc.Weathers.datasupplement.service.JcdzService;
import com.jhhc.Weathers.datasupplement.util.*;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author LYQ
 */
@Component
@EnableScheduling
@RequiredArgsConstructor
public class Schedule {

    private final Params params;

    private final JcdzService jcdzService;

    /**
     * 中科曙光数据下载,输出风场数据文件12,雨量数据入库
     * 168
     */
    //@Scheduled(fixedDelay = 3600000)
    public void dataDownloadZ(){
        String[] strings = new String[]{"00","12"};
        //FileUtil fileUtil = new FileUtil();
        File file;
        for (String string : strings) {
            List<String> times = Output.ybTimes(string);
            for (String time : times) {
                System.out.println(time);
                String path = params.getPath() + File.separator + time + File.separator + "PRATEsfc.nc";
                file = new File(path);
                if (file.exists()) {
                    //雨量数据入库
                    List<BdmsWfProductF> fList = NC_util.readDate2(path, params.getProvideIdZ());
                    if (fList != null) {
                        int fs = jcdzService.selectSG(String.valueOf(fList.get(0).getProvideid()),
                                fList.get(0).getGridnm(), fList.get(0).getRtm());
                        if (fs > 0) {
                            System.out.println("数据已存在");
                        } else {
                            int num = jcdzService.insertBatch(fList);
                            System.out.println("预报时间：" + fList.get(0).getRtm() + "，雨量数据入库完成：" + num);
                        }
                    }
                }else {
                    System.out.println("文件不存在:" + path);
                }
            }
        }
    }

    /**
     * 集成电子数据下载
     * 102
     */
    //@Scheduled(fixedDelay = 600000)
    public void dataDownloadJ(){
        String token = GettingData.getToken();
        GettingData gettingData = new GettingData();
        String[] basinName = params.getBasinNames().split("/");
        String dataType;
        TimeUtil timeUtil = new TimeUtil();
        String time = timeUtil.currentTime();
        int hour = timeUtil.currentHour(time);
        if (hour <= 20) {
            dataType = "08-08";
        }else {
            dataType = "20-20";
        }
        ProductF times = StructureTime.ybTimes(dataType);
        int count = jcdzService.delete(params.getProvideid(),"China5km", StructureTime.timeConstruction(times.getQbTime()));
        System.out.println("删除" + count + "条数据");
        for (String liuyu : basinName) {
            System.out.println(liuyu + "流域");
            for (String ybTime : times.getYbTime()) {
                List<BdmsWfProductF> fs = gettingData.downloadData(token,liuyu, dataType, ybTime,
                        Integer.parseInt(params.getProvideid()), times.getQbTime());
                if (fs != null && !fs.isEmpty()) {
                    int num = jcdzService.insertBatch(fs);
                    System.out.println("预报时间：" + times.getQbTime() + "，雨量数据入库完成：" + num);
                }else {
                    System.out.println("获取数据失败");
                }
            }
            System.out.println(liuyu + "流域入库完成");
        }
    }

}